package cn.Liujialin.org.controller;

import cn.Liujialin.basic.util.AjaxResult;
import cn.Liujialin.org.domain.SysLog;
import cn.Liujialin.org.service.ISysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/log")
public class TestController {

    @Autowired
    private ISysLogService sysLogService;

 /*   @Log("执行方法一")
    @GetMapping("/one")
    public void methodOne(String name){}

    @Log("执行方法二")
    @GetMapping("/two")
    public void methodTwo() throws InterruptedException {
        Thread.sleep(2000);
    }

    @Log("执行方法三")
    @GetMapping("/three")
    public void methodThree(String name,String age){}

    @Log("删除")
    @DeleteMapping("/{id}")
    public void deleteById(Long id){

    }
    @Log("查询")
    @GetMapping
    public List<SysLog> ByAll(){

            return sysLogService.findByAll();

    }*/

    //添加和修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SysLog SysLog) {
        try {
            if (SysLog.getId() == null) {
                sysLogService.save(SysLog);
            } else {
                sysLogService.update(SysLog);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 接口三要素：
     * 1.请求地址是什么 http://localhost:8080/dept/1  delete
     * 2.请求参数是什么 /{id}
     * 3.返回值是什么    AjaxResult
     * @param id
     * @return
     */
    //删除

    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {

            sysLogService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败");
        }
    }

    //获取一条数据
    @GetMapping("/{id}")
    public SysLog queryById(@PathVariable("id") Long id) {
        return sysLogService.findById(id);

    }

    //查询所有

    @GetMapping
    public List<SysLog> queryAll() {
        return sysLogService.findByAll();
    }




}
